<template>
	<view>
		<tn-nav-bar :isBack='true' :backTitle='" "' bottomShadow :alpha="alpha">开始激活</tn-nav-bar>
		<image src="/static/product/topbg.png" class="topbg" mode=""></image>
		<view :style="{paddingTop: vuex_custom_bar_height + 'px'}" class="con">
			<view class="con__box">
				<view class="list-title-container">
					<image src="/static/product/wj.png" class="list-title-container-icon" mode=""></image>
					上传资料
				</view>
				<!-- 表单 -->
				<tn-form :model="form" ref="form" :errorType="['message']" :labelWidth='136'
					:labelStyle='{color:"#6D6D6D",fontSize:"26rpx"}'>
					<tn-form-item label="手机号" prop="phone">
						<tn-input v-model="form.phone" placeholder='请输入手机号'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="姓名" prop="realname">
						<tn-input v-model="form.realname" placeholder='请输入真实姓名'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="卡号" prop="card_no">
						<tn-input v-model="form.card_no" placeholder='请输入卡号'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="密码" prop="pass">
						<tn-input v-model="form.pass" type='password' placeholder='请输入卡号密码'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="出游路线">
						<tn-input v-model="scene" placeholder='请输入出游路线'
							placeholderStyle='color:#6D6D6D;font-size:26rpx;' />
					</tn-form-item>
					<tn-form-item label="上传图片" labelPosition='top'>
						<view>
							<view v-for="(item,index) in lists" :key="index" class="tn-image-upload__custom-btn">
								<view class="cha" @click="del">
									<view class="tn-icon-cross-fill">
										
									</view>
								</view>
								<image class="tn-image-upload__custom-btn" :src="item.url || item.path"
									mode="aspectFill"></image>
							</view>
						</view>
						<tn-image-upload  v-if="!lists.length>0" ref="imageUpload" :action="action" :autoUpload="true" :customBtn="true"
							:showUploadList="true" @on-list-change="listChange" :header='header' :maxCount='1' :multiple='false'>
							<view slot="addBtn" class="tn-image-upload__custom-btn" hover-class="tn-hover-class"
								hover-stay-time="150" style="position: relative;">
								<image src="/static/product/up.png" class="up" mode=""></image>
								<view>相关照片凭证</view>
							</view>
						</tn-image-upload>
					</tn-form-item>
				</tn-form>
			</view>
			<view class="tn-flex tn-flex-col-center xieyi">
				<view class="checkbox">
					<image src="/static/product/gou.png" class="checkbox__icon" mode=""></image>
				</view>
				我已阅读并遵守<text style="color: #49CE81;" @click="to('/pages/my/xiyi?name=userSerive&title=飞象云游服务条款')">《用户协议》</text>与<text style="color: #49CE81;" @click="to('/pages/my/xiyi?name=secret&title=飞象云游隐私政策')">《隐私协议》</text>
			</view>
			<view class="tn-flex tn-flex-row-center">
				<tn-button @click="submit" :loading='btnloading' fontColor="#FFFFFF" shape='round' width='604rpx' height='99rpx' :fontSize='36' backgroundColor="tn-bg-lv" shadow>激活</tn-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {BASE_URL} from '@/api/config.js'
	import product from '@/api/_product.js'
	export default {
		data() {
			return {
				alpha: true,
				form: {
					phone: '',
					realname: '',
					card_no: '',
					pass: '',
					scene_json: '',
					images: ''
				},
				scene:'',
				header:{
					token:uni.getStorageSync('lifeData').vuex_token || ''
				},
				action: BASE_URL+'/api/common/upload',
				rules: {
					realname: [{
						required: true,
						message: '请输入姓名',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					phone: [{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// this.$tn.test.mobile()就是返回true或者false的
							return this.$tn.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['change', 'blur'],
					},],
					card_no: [{
						required: true,
						message: '请输入卡号',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					pass: [{
						required: true,
						message: '请输入密码',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
					scene_json: [{
						required: true,
						message: '请输入出游路线',
						// 可以单个或者同时写两个触发验证方式 
						trigger: ['blur'],
					}],
				},
				lists: [],
				btnloading:false
			};
		},
		onReady() {
			this.$refs.form.setRules(this.rules);
		},
		onLoad(o) {
			product.cardInfoDetail({id:o.id}).then(res=>{
				if(res.data.code==1){
					// this.form.card_no=res.data.data
					console.log(res)
					this.form.card_no=res.data.data.card_no
					this.scene=res.data.data.sceneInfo.title
					this.form.scene_json=res.data.data.sceneInfo.id
				}
			})
		},
		methods: {
			del(){
				this.lists=[]
			},
			submit() {
				let _this=this
				let files = [];
				// 通过filter，筛选出上传进度为100的文件(因为某些上传失败的文件，进度值不为100，这个是可选的操作)
				
				
				this.$refs.form.validate(valid => {
					if (valid) {
						if(this.lists.length==0){
							this.$tn.message.toast('请上传凭证')
							return
						}
						console.log(_this.lists[0].response.data.url)
						_this.form.images=_this.lists[0].response.data.url
						_this.btnloading=true
						
						product.addValid(_this.form).then(res=>{
							if(res.data.code==1){
								_this.$tn.message.toast('激活成功')
								setTimeout(()=>{
									_this.btnloading=false
									_this.backpage()
								},1000)
							}else{
								_this.btnloading=false
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
			upload() {
				this.$refs.imageUpload.upload()
			},
			backpage() {
			
				// 通过判断当前页面的页面栈信息，是否有上一页进行返回，如果没有则跳转到首页
				const pages = getCurrentPages()
				if (pages && pages.length > 0) {
					const firstPage = pages[0]
					if (pages.length == 1 && (!firstPage.route || firstPage.route != 'pages/index/index')) {
						uni.reLaunch({
							url: '/pages/product/card'
						})
					} else {
						uni.navigateBack({
							delta: 1
						})
					}
				} else {
					uni.reLaunch({
						url: '/pages/product/card'
					})
				}
			},
			listChange(lists, index) {
				console.log('上传文件列表发生改变', lists[0].response, index);
				this.lists = lists
				
				
			}
		}
	}
</script>

<style lang="scss">
	.con {
		padding: 30rpx;
		position: relative;
		
		&__box {
			width: 690rpx;
			background: #FFFFFF;
			box-shadow: 0rpx 3rpx 57rpx 0rpx rgba(0, 0, 0, 0.04);
			border-radius: 30rpx;
			margin-top: 50rpx;
			overflow: hidden;
			padding-left: 30rpx;
			padding-bottom: 10rpx;
			.list-title-container {
				display: flex;
				align-items: center;
				font-family: PingFang SC;
				font-weight: bold;
				font-size: 30rpx;
				color: #020202;
				line-height: 30rpx;
				padding: 48rpx 0 20rpx 0;

				&-icon {
					width: 40rpx;
					height: 40rpx;
					margin-right: 10rpx;
				}
			}
		}
	}

	.tn-border-solid-bottom {
		border-color: #F9F9F9;
		border-bottom-width: 2rpx !important;
	}

	.tn-form-item {
		line-height: 72rpx;
		padding: 28rpx 10rpx 28rpx 0;

		/deep/.tn-input__input {
			font-size: 28rpx;
		}
	}

	.tn-image-upload__custom-btn {
		width: 284rpx;
		height: 168rpx;
		background: rgba(154, 154, 154, .1);
		border-radius: 20rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #9A9A9A;
		line-height: 24rpx;
		position: relative;
		.up {
			width: 46rpx;
			height: 46rpx;
			margin-bottom: 26rpx;
		}
	}
	.checkbox{
		width: 28rpx;
		height: 28rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-right: 9rpx;
		&__icon{
			width: 28rpx;
			height: 28rpx;
		}
	}
	.xieyi{
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 24rpx;
		color: #888888;
		line-height: 24rpx;
		margin: 41rpx 0 54rpx;
		padding-left: 5rpx;
	}
	.cha{
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background-color: #020202;
		position: absolute;
		top: 0;
		right: 0;
		z-index: 2;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>